<template>
  <div id="vehicle-group-info">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item">
          <router-link to="/admin/vehicleGroups">车型组信息</router-link>
        </li>

        <li class="breadcrumb-item ms-auto">
          <router-link to="/admin/vehicleGroups/mappings"
            >车型组映射</router-link
          >
        </li>
      </ol>
    </nav>
    <div class="card">
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <input
              type="text"
              class="form-control"
              size="8"
              placeholder="名称"
              v-model.trim="name0"
            />
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-muted btn-sm ms-5"
              @click.stop="reset0()"
            >
              重置
            </button>

            <a
              href="javascript:void(0)"
              class="btn btn-success float-end"
              @click="newVehicleGroup()"
              >新建</a
            >
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-hover table-sm">
      <thead>
        <tr>
          <th>ID</th>
          <th>名称</th>
          <th>等级</th>
          <th>备注</th>
          <th>生成时间</th>
          <th>修改时间</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="(info, index) in dataList"
          :key="info.id"
        >
          <td>{{ info.id }}</td>
          <td>{{ info.name }}</td>
          <td>{{ info.level }}</td>
          <td>{{ info.remark }}</td>
          <td>{{ info.createTime }}</td>
          <td>{{ info.lastUpdate }}</td>
          <td>
            <button class="btn btn-sm" @click.stop="edit(index)">修改</button>
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <!-- New  Modal -->
    <div
      class="modal"
      id="modalNew"
      tabindex="-1"
      role="dialog"
      aria-labelledby="myModalLabel"
      aria-hidden="true"
    >
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title" id="myModalLabel">新建车型组信息</h4>
            <button type="button" class="close" data-dismiss="modal">
              <span aria-hidden="true">&times;</span
              ><span class="sr-only">Close</span>
            </button>
          </div>
          <div class="modal-body"></div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              取消
            </button>
            <button
              type="button"
              class="btn btn-primary"
              v-on:click="saveVehicleGroupInfo()"
            >
              保存
            </button>
          </div>
        </div>
      </div>
    </div>
    <my-modal-vehicle-group ref="vehicleGroupModal"></my-modal-vehicle-group>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import MyPagination from "@/components/my-pagination.vue";
import $ from "jquery";
import {
  searchVehicleGroupInfos,
  saveVehicleGroupInfo,
} from "@/api/basic-data.js";
import MyModalVehicleGroup from "@/components/modal/my-modal-vehicle-group.vue";

export default {
  components: {
    MyPagination,
    MyModalVehicleGroup,
  },
  data() {
    return {
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },
      name0: "",

      

      dataList: [],
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    ...mapMutations(["showLoading", "hideLoading"]),
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.name0,
      };

      this.showLoading();

      searchVehicleGroupInfos(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    reset0: function () {
      this.name0 = "";
    },
    newVehicleGroup: function () {
      const params = {
        id: 0,
        name: "",
        remark: "",
        level: 100,
      };

      this.$refs.vehicleGroupModal.modal(params).then(v => {this.save(v)});
    },
    edit: function (index) {
      const info = this.dataList[index];
      const params = {
        id: info.id,
        name: info.name,
        remark: info.remark,
        level: info.level,
      };

      this.$refs.vehicleGroupModal.modal(params).then(v => {this.save(v)});
    },
    save: function (params) {
      saveVehicleGroupInfo(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.showErrMsg("保存成功");
          this.search();
        } else {
          this.showErrMsg("失败：" + jsonResult.errmsg, "danger");
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
